﻿using PLC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pumptester
{
    public partial class Form2 : Form
    {
        Thread t;
        System.Timers.Timer timer;
        public Form2()
        {
            InitializeComponent();
            t = new Thread(new ThreadStart(GetData));
            t.IsBackground = true;
            t.Start();
           
        }

        private void GetData()
        {
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Enabled = false;
            timer.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；  
            //timer.Start();
            timer.Elapsed += (o, a) =>
            {
                Thread.Sleep(1000);
                SetData();
                ShowMessage(string.Format("更新时间：" + DateTime.Now));
            };
        }
        private delegate void SetDataDelegate();
        private void SetData()
        {
            if (this.InvokeRequired)
            {
                if (this.IsHandleCreated)
                {
                    this.Invoke(new SetDataDelegate(SetData));
                }
                
            }
            else
            {
                //Thread.Sleep(1000);
                //label1.Text = string.Format("更新时间：" + DateTime.Now);
            }
        }
        private delegate void ShowMessageDelegate(string message);
        private void ShowMessage(string message)
        {
            if (this.InvokeRequired)
            {                
                ShowMessageDelegate showMessageDelegate = ShowMessage;
                if(this.IsHandleCreated)
                {
                    this.Invoke(showMessageDelegate, new object[] { message });
                }
            }
            else
            {
 
                //this.textBox1.Text = message;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //timer.Stop();
            //timer.Enabled = false;
            //SerialPortManager serialPortManager= SerialPortManager.GetInstance();
            //serialPortManager.Open();
            //serialPortManager.readData();
           // MessageBox.Show("hello");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //timer.Start();
           // timer.Enabled = true;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
           // Console.WriteLine("11111");
        }

        private void ucPanelTitle1_Load(object sender, EventArgs e)
        {

        }
    }
}
